package com.google.gwt.it2tool.client.widget;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import com.google.gwt.it2tool.client.ClientDataCache.DataChangeHandler;
import com.google.gwt.it2tool.client.Factory;
import com.google.gwt.it2tool.client.Production;
import com.google.gwt.it2tool.client.Recipe;

public class RessourceNeedsFlexTable extends ProductFlexTable {
	
	private List<Factory> specialDataSet;

	public RessourceNeedsFlexTable(){
		super();	
		
		setDataChangeHandler(dataChangeHandler);
		
		setText(0, 0, "Produkt");
		setText(0, 1, "Bedarf/24h");
		getRowFormatter().addStyleName(0, "factoryListHeader");
		getCellFormatter().addStyleName(0, 0, "summaryListProductHeader");
		getCellFormatter().addStyleName(0, 1, "summaryListNumberHeader");
		addStyleName("summaryList");
		setCellPadding(6);
	}
	
	public void setData(List<Factory> dataSet){
		specialDataSet=dataSet;
		update();
	}
	
	private void update(){
		//Input
		Iterator<Factory> factories;
		if (specialDataSet==null){
			factories = dataCache.getFactories().values().iterator();
		}else{
			factories = specialDataSet.iterator();
		}
		HashMap<String,Recipe> recipes = dataCache.getRecipes();
		int massProdLvl = dataCache.getMassProdLevel();
		
		//Output
		Map<String,Integer> ressourceNeeds = new TreeMap<String,Integer>();
		Map<String,String> ressourceUnits = new TreeMap<String,String>();
		
		Production currProd;
		String currIng;
		Recipe r;
		HashMap<String, Double> ing;
		HashMap<String, String> uni;
		while (factories.hasNext()){
			Iterator<Production> iterProds = factories.next().getProductions().iterator();
			while (iterProds.hasNext()){
				currProd=iterProds.next();
				r = recipes.get(currProd.getName());
				if (r!=null){
					ing = r.getIngridients();
					uni = r.getUnits();
					Iterator<String> iter = ing.keySet().iterator();
					while(iter.hasNext()){
						currIng = iter.next();
						if (!ressourceNeeds.containsKey(currIng)){
							ressourceNeeds.put(currIng, (int) ((currProd.getOutput()/currProd.getProductivity())*ing.get(currIng)));
							ressourceUnits.put(currIng, uni.get(currIng));
						}else{
							int old = ressourceNeeds.get(currIng);
							ressourceNeeds.put(currIng,old + (int) ((currProd.getOutput()/currProd.getProductivity())*ing.get(currIng)));
						}
					}
				}			
			}
		}
		
		int nrOfRows = getRowCount();
		for (int i=0;i<nrOfRows-1;i++){
			removeRow(getRowCount()-1);
		}
		if (ressourceNeeds.size()==0){
			setText(1,0,"Keine Rezepte gefunden");
		}else{
			String[] names = new String[ressourceNeeds.size()];
			ressourceNeeds.keySet().toArray(names);
			int size = names.length;
			
			for(int i=0;i<size;i++){
				setWidget(i+1, 0, getProductLabel(new Production(names[i])));
				//setText(i+1, 0, names[i]);
				setText(i+1, 1, ressourceNeeds.get(names[i]).toString()+" "+ressourceUnits.get(names[i]));
				getCellFormatter().addStyleName(i+1,1,"productionOutput");
				getCellFormatter().addStyleName(i+1,1,"factoryListNumericColumn");
			}
		}		
	}
	
	private DataChangeHandler dataChangeHandler = new DataChangeHandler() {
		
		@Override
		public void onRecipeDataChanged() {
			update();
			
		}
		
		@Override
		public void onPrototypeDataChanged() {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void onMassProductionDataChanged() {
			update();			
		}
		
		@Override
		public void onFactoryDataChanged() {	
			update();
		}
	};

}
